<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2019/12/15
 * Time: 14:55
 */

namespace app\admin\controller;


use app\admin\validate\ManageValidate;
use app\common\traits\ModelTrait;
use app\common\utils\JsonService;
use app\admin\model\Manage as ManageModel;
use app\common\model\BaseModel;
use app\admin\model\Role;
use app\common\utils\Random;
use think\facade\View;
use think\Facade\Session;

class Manage extends Base
{
    /**
     * 添加管理员
     * @return mixed
     */
    public function create(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new ManageValidate;
            if(!$validate->scene('add')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return  ManageModel::createManage($post);
        }

        $roleData = Role::getAllRole();
        return View::fetch('create',['roleData'=>$roleData]);
    }

    /**
     * 编辑管理员
     * @return mixed
     */
    public function update(){
        if($this->request->isAjax()){
            $return = $this->return;
            $post = $this->request->post();

            $validate = new ManageValidate();
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return ManageModel::updateManage($post);
        }
        $id = $this->request->get('id');
        $model = $this->findModel($id);
        $roleData = Role::getAllRole();
        return View::fetch('update',[
            'model'=>$model,
            'roleData'=>$roleData,
        ]);
    }

    /**
     * 管理员列表
     * @return mixed
     */
    public function index(){

        $dataProvider = ManageModel::manageList();
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }

    /*
     * 删除管理员
     * */
    public function delete(){
        if($this->request->isAjax()){
            $id = $this->request->post('id');
            try{
                $model = $this->findModel($id);
                $model->delete();
            }catch (\Exception $ex){
              return JsonService::error($ex->getMessage());
            }
           return JsonService::success('删除成功');
        }
    }


    /*
     * 修改密码
     * */
    public function manage_info(){

        if($this->request->isAjax()){
            $post = $this->request->post();

            try {
                if (!empty($post['password'])) {
                    if(md5(md5($post['old_password'].$this->admin_user['salt'])) !== $this->admin_user['password']){
                        return JsonService::error('原密码不正确');
                    }
                    if(md5($post['password']) != md5($post['repassword'])){
                        return JsonService::error('确认密码不一致');
                    }
                    $post['salt'] = Random::alnum();
                    $post['entry_password'] = $post['password'];
                    $post['password'] = md5(md5($post['password'] . $post['salt']));
                }else{
                    unset($post['password']);
                    unset($post['entry_password']);
                }
                ManageModel::updateOne($post, ['id' => $this->admin_id]);
            } catch (\Exception $ex) {
                return JsonService::error($ex->getMessage());
            }
            Session::delete('adminUser');
            Session::delete('admin_id');
            return JsonService::success();
        }
        $model = $this->findModel($this->admin_id);
        return View::fetch('manage_info',['model'=>$model]);
    }

    /**
     * 得到模型
     *
     * @param       $id
     * @param array $where
     *
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }

        return ManageModel::findModel($id, $where);
    }
}